<?php
/**
 * 
 * Class of expense
 * @author bthiep
 *
 */
class ExpenseComponent extends CComponent
{
	/**
	 * Expense status
	 */
	const EXPENSE_UN_SUBMITED = 1;
	const EXPENSE_SUBMITED = 2;
	const EXPENSE_DELETED = 3;
	/**
	 * 
	 * get fisrt page initialization data
	 * @return array
	 */
	public function getFirstPageData()
	{
		$userId = Yii::app()->user->id;
		$commandCustomers = Yii::app()->db->createCommand("EXEC stp_GetCustomersByUserId :UserId");
		$commandCustomers->bindValue(':UserId',$userId,PDO::PARAM_INT);
		$customers = $commandCustomers->queryAll();
		$currencies = Yii::app()->db->createCommand("EXEC stp_GetAllActivedCurrencies")->queryAll();
		$categories = Yii::app()->db->createCommand("EXEC stp_GetAllActivedExpenseCategoryByUserId $userId")->queryAll();
		$commandReport = Yii::app()->db->createCommand("EXEC stp_GetReportsActivedLimited :Limit, :Status, :UserId");
		$limit = Yii::app()->params['reportLimitForExpensePage'];
		$commandReport->bindValue(':Limit', $limit, PDO::PARAM_INT);
		$status = ReportComponent::REPORT_ACTIVED;
		$commandReport->bindValue(':Status', $status, PDO::PARAM_INT);
		$commandReport->bindValue(':UserId', $userId, PDO::PARAM_INT);
		$reports = $commandReport->queryAll();
		$preferences = Yii::app()->user->preferences;
		$defaultCurrency = Yii::app()->db->createCommand("EXEC stp_GetDefaultCurrencyByUserId $userId ")->queryScalar();
		return array(
			'customers' => $customers,
			'currencies' => $currencies,
			'categories' => $categories,
			'reports' => $reports,
			'preferences' => $preferences
		);
	}
	/**
	 * Get search result...
	 * @param int $page
	 * @return array
	 */
	public function getSearchResults($page = 1) 
	{
		$merchant = isset($_POST['merchant']) ? trim($_POST['merchant']) : '';
		$from = isset($_POST['startDate']) ? $_POST['startDate'] : date('Y-m-d G:i:s', PHP_INT_MAX + 1);
		$to = isset($_POST['endDate']) ? $_POST['endDate'].' 23:59:59.999' : date('Y-m-d G:i:s', PHP_INT_MAX);
		$expenseStatus = isset($_POST['expense_filter_status']) ? $_POST['expense_filter_status'] : ExpenseComponent::EXPENSE_UN_SUBMITED;
		$categoryId = isset($_POST['expense_filter_categories']) ? $_POST['expense_filter_categories'] : -1;
	    $customerId = isset($_POST['expense_filter_customers']) ? $_POST['expense_filter_customers'] : -1;
		$pageSize = Yii::app()->params['expensePageSize'];
		$userId = Yii::app()->user->id;
		$expenseForm = isset($_POST['expense_filter_form']) ? $_POST['expense_filter_form'] : 0;
	
		$command = Yii::app()->db->createCommand("EXEC stp_GetAllExpense :Merchant,:From,:To,:ExpenseForm,:ExpenseStatus,:CategoryId,:CustomerId, :UserId,:PageSize,:Page");
		$command->bindValue(':Merchant', $merchant, PDO::PARAM_STR);
		$command->bindValue(':From', $from, PDO::PARAM_STR);
		$command->bindValue(':To', $to, PDO::PARAM_STR);
		$command->bindValue(':ExpenseForm', $expenseForm, PDO::PARAM_INT);
		$command->bindValue(':ExpenseStatus', $expenseStatus, PDO::PARAM_INT);
		$command->bindValue(':CategoryId', $categoryId, PDO::PARAM_INT);
		$command->bindValue(':CustomerId', $customerId, PDO::PARAM_INT);
		$command->bindValue(':UserId', $userId, PDO::PARAM_INT);
		$command->bindValue(':PageSize', $pageSize, PDO::PARAM_INT);
		$command->bindValue(':Page', $page, PDO::PARAM_INT);
		$rows = $command->queryAll();
		return array(
		'TotalRows' =>isset($rows[0]['TotalRows'])?$rows[0]['TotalRows']:0,
		'Rows' =>!empty($rows)?$rows:array()
		);
		
	}
	
	public function getNewExpenseFormData()
	{
		
	}
	
}